package gui;

import java.awt.BorderLayout;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import Lexicon.Lexicon;
import Lexicon.MyNode;
import Lexicon.Word;

public class DicTable extends JPanel  {

	private static final long serialVersionUID = 1L;
	public final static int CURRENR = 0 , MODIFY = 1 , NEW = 2;
	int dicState ;
	String[] columnNames = 
	{ 
			ArMT.labels.getString("arWord"), 
			ArMT.labels.getString("enWord"),
			ArMT.labels.getString("type"),
			ArMT.labels.getString("num"),
			ArMT.labels.getString("gen"),
	};
	Lexicon lex ;
	static Vector<Vector<String>> rows ;  //  @jve:decl-index=0:
	static Vector<String> columns ;
	Vector dataRows  ;
	static JTable wordTable ;
	static DefaultTableModel data ;
	/**
	 * This is the default constructor
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 * @throws SAXException 
	 */
	public DicTable() throws SAXException, IOException, ParserConfigurationException {
		rows = new Vector<Vector<String>>(); 
		columns = new Vector<String>();
		dataRows = new Vector() ;
		//data = new DefaultTableModel(rows,columns) ;
		lex = new Lexicon();
		setDicState(CURRENR);
		if(getDicState() == CURRENR)
		{
		dataRows = Lexicon.wordTree.traverseInOrder(Lexicon.wordTree.getRoot(),dataRows);
		 Enumeration en = dataRows.elements(); 
		 int i = 0;
		 MyNode []nodes =  new MyNode[dataRows.size()]   ;	 
		 Word w = new Word() ; 
		 while(en.hasMoreElements())
		 { 
			 nodes[i] = (MyNode) en.nextElement();
//			 String [] s = {nodes[i].getArabicWord(),
//					 nodes[i].getEnglishWord(),
//					 w.toString(nodes[i].getType()),
//					 w.toString(nodes[i].getNumber()),
//					 w.toString(nodes[i].getGender())
//					 };
//			 data.addRow(s);
			 
			 Vector<String> r=new Vector<String>(5);
			    r.add(nodes[i].getArabicWord());
			    r.add(nodes[i].getEnglishWord());
			    r.add(w.toString(nodes[i].getType()));
			    r.add(w.toString(nodes[i].getNumber()));
			    r.add(w.toString(nodes[i].getGender()));
			 rows.add(r);
			 i++ ;
			 //addRow(nodes[i]) ;
			// s[i] = nodes[i].getArabicWord() ;   		 
		 }
		}
		else if(getDicState() == MODIFY )
		{
			dataRows = Lexicon.searchTree.traverseInOrder(Lexicon.searchTree.getRoot(),dataRows);
			 Enumeration en = dataRows.elements(); 
			 int i = 0;
			 MyNode []nodes =  new MyNode[dataRows.size()]   ;	 
			 Word w = new Word() ; 
			 while(en.hasMoreElements())
			 { 
//				 nodes[i] = (MyNode) en.nextElement();
//				 String [] s = {nodes[i].getArabicWord(),
//						 nodes[i].getEnglishWord(),
//						 "-",
//						 "-",
//						 "-"
//						 };
//				 data.addRow(s);
				 Vector<String> r=new Vector<String>(5);
				    r.add(nodes[i].getArabicWord());
				    r.add(nodes[i].getEnglishWord());
				    r.add("-");
				    r.add("-");
				    r.add("-");
				 rows.add(r);
				 i++ ;
		}
		} else if(getDicState() == NEW )
				{
//			String [] s = {" ",
//					 " ",
//					 " ",
//					 " ",
//					 " "
//					 };
//				data.addRow(s);		
			Vector<String> r=new Vector<String>(5);
		    r.add(" ");
		    r.add(" ");
		    r.add(" ");
		    r.add(" ");
		    r.add(" ");
		 rows.add(r);
				} 
		 for(int j=0 ; j<columnNames.length ; j++)
			columns.add((String) columnNames[j]);
		 //addColumns(columnNames); 
		 setLayout(new BorderLayout());
		 data = new DefaultTableModel(rows,columns) ;
		 wordTable = new JTable(data);
		 add(wordTable.getTableHeader(), BorderLayout.PAGE_START);
		 add(wordTable , BorderLayout.CENTER );
		 ArMT.applyComponentOrientation(wordTable, ArMT.co);
		 setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder(ArMT.labels.getString("CurrentDict")), BorderFactory.createEmptyBorder(5,5,5,5)
					)
					);
		// wordTable.repaint();
	}
	public JTable getWordTable()
	{
		return wordTable ; 
	}

	 public static void addNewRow(String ar , String en ,String ty, String num ,String gen)
		{
//		 Vector r=new Vector(5);
//		    r.add(ar);
//		    r.add(en);
//		    r.add(ty);
//		    r.add(num);
//		    r.add(gen);
//		 rows.add(r);
		 String [] s = {ar,en,ty,num,gen};
		 data.addRow(s);
		// wordTable.repaint();
		}
	 public static void removeSelectedRow (int index)
	 {
		 data.removeRow(index) ;
	 }
	 public static int getSelectedRow()
	 {
		 return wordTable.getSelectedRow();
	 }
	 public static String getDataAt(int rowIndex)//for Binary tree delete
	 {
		 return data.getValueAt(wordTable.getSelectedRow(),0/*we need arabic word only*/).toString() ;
	 }
	public void tableChanged(TableModelEvent arg0) {
		// TODO Auto-generated method stub
	}
	public void setDicState(int dicState) {
		this.dicState = dicState;
	}
	public int getDicState() {
		return dicState;
	}
}
